package com.ztools.pc.service.impl.store;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztools.common.model.pc.information.WeChatMember;
import com.ztools.common.model.pc.store.Commodity;
import com.ztools.common.model.pc.store.StoreSale;
import com.ztools.common.util.common.PageUtil;
import com.ztools.common.util.common.ServiceException;
import com.ztools.common.util.pc.DictUtil;
import com.ztools.pc.mapper.store.StoreSaleMapper;
import com.ztools.pc.service.coupon.CouponTemplateService;
import com.ztools.pc.service.information.MemberService;
import com.ztools.pc.service.store.CommodityService;
import com.ztools.pc.service.store.StoreSaleService;
import com.ztools.pc.service.sys.DictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author seeTools
 * @since 2023-06-04
 */
@Service
public class StoreSaleServiceImpl extends ServiceImpl<StoreSaleMapper, StoreSale> implements StoreSaleService {

    @Resource
    private CommodityService commodityService;
    @Resource
    private MemberService memberService;
    @Resource
    private DictService dictService;
    @Resource
    private CouponTemplateService couponTemplateService;

    /**
     * 分页查询
     *
     * @param pageUtil 分页数据
     * @return 对象
     */
    @Override
    public Page<StoreSale> findList(PageUtil<StoreSale> pageUtil) {
        List<OrderItem> orders = new ArrayList<>();
        QueryWrapper<StoreSale> queryWrapper = getQueryWrapper(pageUtil.getParams());
        Page<StoreSale> pages = new Page<>(pageUtil.getPage(), pageUtil.getSize());
        // 排序数据  StrUtil.toUnderlineCase huTools将驼峰装换为下划线格式
        orders.add(new OrderItem(StrUtil.toUnderlineCase(pageUtil.getSortName()), "asc".equals(pageUtil.getSortOrder())));
        pages.setOrders(orders);
        Page<StoreSale> page = page(pages, queryWrapper);
        dictionary(page.getRecords());
        return page;
    }

    @Override
    public List<StoreSale> findByObject(StoreSale storeSale) {
        QueryWrapper<StoreSale> queryWrapper = new QueryWrapper<>(storeSale);
        queryWrapper.orderByDesc("id");
        List<StoreSale> list = list(queryWrapper);
        dictionary(list);
        return list;
    }

    @Override
    public boolean update(StoreSale storeSale) {
        boolean b = false;
        // 修改前先获取，在删除（乐观锁）
        StoreSale byId = getById(storeSale);
        b = updateById(storeSale);
        return b;
    }

    @Override
    public boolean delete(StoreSale storeSale) {
        boolean b = false;
        StoreSale byId = getById(storeSale);
        b = removeById(storeSale);
        return b;
    }

    public QueryWrapper<StoreSale> getQueryWrapper(StoreSale storeSale) {
        QueryWrapper<StoreSale> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(storeSale.getCommodityName())) {
            List<Commodity> commodityList = commodityService.findByName(storeSale.getCommodityName());
            if (commodityList.size() == 0) {
                throw new ServiceException("不存在该商品");
            }
            List<Long> ids = commodityList.stream().map(Commodity::getId).collect(Collectors.toList());
            queryWrapper.lambda().in(StoreSale::getCommodityId, ids);
        }
        if (StrUtil.isNotEmpty(storeSale.getPaymentNo())) {
            queryWrapper.lambda().eq(StoreSale::getPaymentNo, storeSale.getPaymentNo());
        }
        if (storeSale.getCommodityId() != null) {
            queryWrapper.lambda().eq(StoreSale::getCommodityId, storeSale.getCommodityId());
        }
        if (StrUtil.isNotEmpty(storeSale.getPayType())) {
            dictService.findByTypeCode(DictUtil.PAY_TYPE, storeSale.getPayType());
            queryWrapper.lambda().eq(StoreSale::getPayType, storeSale.getPayType());
        }
        if (storeSale.getPayDate1() != null) {
            queryWrapper.lambda().le(StoreSale::getPayDate, storeSale.getPayDate1());
        }
        if (storeSale.getPayDate2() != null) {
            queryWrapper.lambda().gt(StoreSale::getPayDate, storeSale.getPayDate2());
        }
        return queryWrapper;
    }

    /**
     * 字典翻译
     *
     * @param storeSaleList 对象集合
     */
    public void dictionary(List<StoreSale> storeSaleList) {
        for (StoreSale storeSale : storeSaleList) {
            dictionary(storeSale);
        }
    }

    public void dictionary(StoreSale storeSale) {
        if (storeSale != null) {
            if (storeSale.getCommodityId() != null) {
                Commodity commodity = commodityService.getById(storeSale.getCommodityId());
                storeSale.setPicUrls(commodity.getPicUrls());
                storeSale.setCommodityName(commodity.getName());
            }
            if (storeSale.getMemberId() != null) {
                WeChatMember member = memberService.getById(storeSale.getMemberId());
                storeSale.setMemberName(member.getNickName());
            }
            if (storeSale.getPayType() != null) {
                storeSale.setPayTypeName(dictService.findByTypeCode(DictUtil.PAY_TYPE, storeSale.getPayType() + ""));
            }
        }
    }

}

